﻿// <copyright file="TestContosoWebAnalytics.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2009-06-04</date>
// <summary>Summary of this file.</summary>

namespace Contoso.WebAnalytics.Test
{
    using System;
    using System.ComponentModel.Composition;
    using System.Linq;
    using System.Windows;
    using Microsoft.Silverlight.Testing;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Microsoft.WebAnalytics;

    /// <summary>
    /// Unit test class for Contoso.WebAnalytics.ContosoAnalytics
    /// </summary>
    [TestClass]
    public class TestContosoWebAnalytics : SilverlightTest
    {
        /// <summary>
        /// The Contoso analytics service
        /// </summary>
        private ContosoAnalytics analytics;

        /// <summary>
        /// Initializes a new instance of the TestContosoWebAnalytics class.
        /// </summary>
        public TestContosoWebAnalytics()
        {
            CompositionInitializerEx.SatisfyImports(this);
        }

        #region Properties
        /// <summary>
        /// Gets or sets the event Logging method
        /// </summary>
        /// <remarks>This is imported from the Microsoft.WebAnalytics assembly using MEF.</remarks>
        [Import("Log")]
        public Action<AnalyticsEvent> Log { get; set; }

        /// <summary>
        /// Gets the Contoso analytics service
        /// </summary>
        private ContosoAnalytics Analytics
        {
            get
            {
                if (this.analytics == null)
                {
                    var lifetimeObjects = (from lifetimeObject in Application.Current.ApplicationLifetimeObjects.OfType<WebAnalyticsService>()
                                           select lifetimeObject).FirstOrDefault();

                    this.analytics = (from service in lifetimeObjects.Services.OfType<ContosoAnalytics>()
                                      select service).FirstOrDefault();
                }

                return this.analytics;
            }
        }
        #endregion

        #region Methods

        /// <summary>
        /// Test setting name and object name
        /// </summary>
        [TestMethod]
        public void TestNameObjectName()
        {
            var logEvent = new AnalyticsEvent()
            {
                Name = "TestContosoWebAnalytics",
                ObjectName = "TestNameObjectName"
            };

            this.Log(logEvent);
        }

        /// <summary>
        /// Test a null event log
        /// </summary>
        [ExpectedException(typeof(ArgumentNullException)), 
         TestMethod]
        public void TestLogNull()
        {
            this.Log(null);
        }

        /// <summary>
        /// Test setting nothing
        /// </summary>
        [TestMethod]
        public void TestEmpty()
        {
            var logEvent = new AnalyticsEvent();

            this.Log(logEvent);
        }
        #endregion
    }
}
